//
//  SWHomePageCollectionTableViewCell.m
//  sc-cyan-ios
//
//  Created by lvjian01 on 2017/12/21.
//  Copyright © 2017年 greenbuilding. All rights reserved.
//

#import "SWHomePageCollectionTableViewCell.h"
#import "SWHomeTabCellCollectionViewCell1.h"
#import "SWHomeTabCellCollectionViewCell2.h"
#import "SWHomeTabCellCollectionViewCell3.h"

@interface SWHomePageCollectionTableViewCell ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collectionView;



@end

@implementation SWHomePageCollectionTableViewCell


//现在死数据，后期使用同一传值，使用统一的mode;
- (NSArray *)sectionZeroLists{
    return @[
             @{@"name":@"租赁信息",@"image":@"sy_a_zulin"},
             @{@"name":@"求租信息",@"image":@"sy_b_qiuzu"},
             @{@"name":@"租赁企业库",@"image":@"sy_c_qiye"},
             ];
}

- (NSArray *)sectionTwoLists{
    return @[
             @{@"name":@"产品库",@"image":@"sy_chanpin"},
             @{@"name":@"企业库",@"image":@"sy_qiye"},
             @{@"name":@"项目库",@"image":@"sy_xiangmu"},
             @{@"name":@"专家库",@"image":@"sy_zhuanjia"},
             ];
}

// 没有根据collectionview的宽度进行计算，而是根据屏幕宽度计算
// 后期可以根据屏幕宽度计算collectionview宽度，从而计算cell宽度
- (CGFloat)cellWidth{
    switch (self.cellType) {
        case SWTableCellCollectionCellType1:
            return (Main_Screen_Width - 20) / 3;
        case SWTableCellCollectionCellType2:
            return (Main_Screen_Width - 14 - 20) / 3;
        case SWTableCellCollectionCellType3:
            return (Main_Screen_Width - 25 - 20) / 2;
        default:
            return 0;
    }
}

- (CGFloat)cellHeight{
    switch (self.cellType) {
        case SWTableCellCollectionCellType1:
            return (Main_Screen_Width - 20) / 3;
        case SWTableCellCollectionCellType2:
            return (Main_Screen_Width - 14 - 20) / 3;
        case SWTableCellCollectionCellType3:
            return 40;
        default:
            return 0;
    }
}

//- (UICollectionViewScrollDirection)scrollDerection{
//    switch (self.cellType) {
//        case SWTableCellCollectionCellType1:
//        case SWTableCellCollectionCellType2:
//            return UICollectionViewScrollDirectionHorizontal;
//        case SWTableCellCollectionCellType3:
//            return UICollectionViewScrollDirectionVertical;
//        default:
//            return UICollectionViewScrollDirectionHorizontal;
//    }
//}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self initUI];
    }
    return self;
}

- (instancetype)init{
    if (self = [super init]) {
        [self initUI];
    }
    return self;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self initUI];
    }
    return self;
}

- (void)initUI{
    
    UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout];
    
//    self.backgroundColor = [UIColor greenColor];
    self.collectionView.backgroundColor = [UIColor clearColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.scrollsToTop = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.showsVerticalScrollIndicator = NO;
//    self.collectionView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 10, 0, 10);
    
    [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([SWHomeTabCellCollectionViewCell1 class]) bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:NSStringFromClass([SWHomeTabCellCollectionViewCell1 class])];
    [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([SWHomeTabCellCollectionViewCell2 class]) bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:NSStringFromClass([SWHomeTabCellCollectionViewCell2 class])];
    [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([SWHomeTabCellCollectionViewCell3 class]) bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:NSStringFromClass([SWHomeTabCellCollectionViewCell3 class])];
    
//    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader
//                   withReuseIdentifier:NSStringFromClass([UICollectionReusableView class])];
    
    [self addSubview:self.collectionView];
    
    
}

- (void)setCellType:(SWTableCellCollectionCellType)cellType{
    _cellType = cellType;

    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self).with.insets([self edgeInsets]);

        make.height.mas_equalTo([self collectionViewHeight]);
    }];
    
    [self addHeader];
//    UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)
//    self.collectionView.collectionViewLayout;
//    layout.scrollDirection = [self scrollDerection];
    
}

- (void)addHeader{
    switch (self.cellType) {
        case SWTableCellCollectionCellType3:{
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, Main_Screen_Width-25, 40)];
            label.text = @"绿建筑平台核心";
            label.textAlignment = NSTextAlignmentCenter;
            label.textColor = [UIColor gb_textColor_999999];
            label.font = SYSTEMFONT(13);
            [self addSubview:label];
        }
            break;
            
        default:
            break;
    }
}

- (CGFloat)collectionViewHeight{
    switch (self.cellType) {
        case SWTableCellCollectionCellType1:
        case SWTableCellCollectionCellType2:
            return [self cellHeight];
        case SWTableCellCollectionCellType3:{
            NSInteger linNum = 2;
            return (linNum-1)*12.5 + linNum*[self cellHeight];
        }
//            return 12.5 + 10 + 2*[self cellHeight];

        default:
            return 0;
            break;
    }
}

- (UIEdgeInsets)edgeInsets{
    switch (self.cellType) {
        case SWTableCellCollectionCellType1:
            return UIEdgeInsetsMake(0, 10, 0, 10);
        case SWTableCellCollectionCellType2:
            return UIEdgeInsetsMake(0, 10, 5, 10);
        case SWTableCellCollectionCellType3:
            return UIEdgeInsetsMake(40, 12.5, 10, 12.5);
        default:
            return UIEdgeInsetsZero;
    }
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    switch (self.cellType) {
        case SWTableCellCollectionCellType1:
            return 3;
        case SWTableCellCollectionCellType2:
            return 3;
        case SWTableCellCollectionCellType3:
            return 4;
        default:
            return 0;
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake([self cellWidth], [self cellHeight]);
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    switch (self.cellType) {
        case SWTableCellCollectionCellType1:{
            
            NSDictionary *datadic = [[self sectionZeroLists] objectAtIndex:indexPath.row];
            SWHomeTabCellCollectionViewCell1 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([SWHomeTabCellCollectionViewCell1 class]) forIndexPath:indexPath];
            
            cell.image = [datadic objectForKey:@"image"];
            cell.name = [datadic objectForKey:@"name"];
            
            return cell;
        }
        case SWTableCellCollectionCellType2:{
            SWHomeTabCellCollectionViewCell2 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([SWHomeTabCellCollectionViewCell2 class]) forIndexPath:indexPath];
            
            return cell;
        }
        case SWTableCellCollectionCellType3:{
            NSDictionary *dataDic = [[self sectionTwoLists] objectAtIndex:indexPath.row];
            SWHomeTabCellCollectionViewCell3 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([SWHomeTabCellCollectionViewCell3 class]) forIndexPath:indexPath];
            cell.image = [dataDic objectForKey:@"image"];
            cell.name = [dataDic objectForKey:@"name"];
            return cell;
        }
        default:
            break;
    }
    
    return nil;
    
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    switch (self.cellType) {
        case SWTableCellCollectionCellType1:
            return 0;
        case SWTableCellCollectionCellType2:
            return 7;
        case SWTableCellCollectionCellType3:
            return 20;
        default:
            return 0;
    }
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    switch (self.cellType) {
        case SWTableCellCollectionCellType1:
            return 0;
        case SWTableCellCollectionCellType2:
            return 0;
        case SWTableCellCollectionCellType3:
            return 12.5;
        default:
            return 0;
    }
    return 0;
}

//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
//    if (self.cellType == SWTableCellCollectionCellType3) {
//        return CGSizeMake(collectionView.width, 40);
//    }
//    return CGSizeZero;
//}

//- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
//    if (kind == UICollectionElementKindSectionHeader) {
//        
//        
//        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:NSStringFromClass([UICollectionReusableView class]) forIndexPath:indexPath];
//        UILabel *label = [[UILabel alloc] init];
//        label.textColor = [UIColor redColor];
//        label.text = @"绿建筑平台核心";
//        [headerView addSubview:label];
//        return headerView;
//        
//    }
//    return nil;
//}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
//    switch (self.cellType) {
//        case SWTableCellCollectionCellType1:{
//            
//        }
//        case SWTableCellCollectionCellType2:
//        case SWTableCellCollectionCellType3:
//        default:
//            break;
//    }
    
    if (self.selectedItemWithIndex) {
        self.selectedItemWithIndex(indexPath.row);
    }
    
}


@end
